package com.mxpio.erp.hr.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.hr.entity.Employee;
import com.mxpio.erp.common.hr.service.EmployeeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.CriteriaUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "EmployeeController", tags = { "人力接口" })
@RestController
@RequestMapping("/erp/hr/employee/")
public class EmployeeController {
	
	@Autowired
	private EmployeeService employeeSerivce;

	@GetMapping("list")
	@ApiOperation(value = "员工列表", notes = "获取员工列表", httpMethod = "GET")
	public Result<List<Employee>> list(String criteria) throws UnsupportedEncodingException {
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		List<Employee> tos = employeeSerivce.list(Employee.class, c);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@ApiOperation(value = "员工列表", notes = "获取员工列表", httpMethod = "GET")
	public Result<Page<Employee>> page(String criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Criteria c = CriteriaUtils.json2Criteria(criteria);
		Page<Employee> page = employeeSerivce.listPage(Employee.class, pageAble, c);
		return Result.OK(page);
	}
	
	@GetMapping("list/{bizNo}")
	@ApiOperation(value = "根据bizNo获取员工", notes = "根据bizNo获取员工", httpMethod = "GET")
	public Result<Employee> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
		Employee employee = employeeSerivce.getById(Employee.class, bizNo);
		return Result.OK(employee);
	}
	
	@PostMapping("add")
	@ApiOperation(value = "新增员工", notes = "新增员工", httpMethod = "POST")
	public Result<Employee> add(@RequestBody Employee employee) {
		employeeSerivce.save(employee);
		return Result.OK(employee);
	}
	
	@PutMapping("edit")
	@ApiOperation(value = "编辑员工", notes = "编辑员工（全量）", httpMethod = "PUT")
	public Result<Employee> edit(@RequestBody Employee employee) {
		employeeSerivce.update(employee);
		return Result.OK(employee);
	}
	
	@DeleteMapping("remove/{bizNos}")
	@ApiOperation(value = "删除员工", notes = "删除员工", httpMethod = "DELETE")
	public Result<Employee> remove(@PathVariable(name = "bizNos", required = true) String bizNos) {
		String bizNo[] = bizNos.split(",");
		for(String key : bizNo){
			employeeSerivce.delete(Employee.class, key);
		}
		return Result.OK();
	}
	
}
